<template>
    <el-container class="page" :class="{ bg: isHome }" direction="vertical">
        <pageHeader :titleList="titleList"></pageHeader>
        <el-container direction="vertical">
            <el-main class="page-main">
                <keep-alive>
                    <router-view
                        :titleList="titleList"
                        :winH="winH"
                        v-if="$route.meta.keepAlive"
                    ></router-view>
                </keep-alive>
                <router-view
                    :titleList="titleList"
                    :winH="winH"
                    v-if="!$route.meta.keepAlive"
                ></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>
<script>
import pageHeader from "@/components/Header.vue"
import { mapState } from "vuex"
export default {
    name: "Main",
    props: {
        winH: {
            type: Number,
            default: 500,
        },
    },
    components: {
        pageHeader,
    },
    watch: {
        $route(to, from) {
            this.isHome = to.path == "/"
        },
    },
    computed: {
        ...mapState(["activeMenuName", "siteInfos"]),
        title() {
            let routeName = this.$route.name
            let title = ""
            switch (routeName) {
                case "notice":
                    title = "通知公告 "
                    break
                case "record":
                    title = "一企一档"
                    break
                case "issue":
                    title = "检查工作下发 "
                    break
                case "implement":
                    title = "检查工作落实 "
                    break
                case "inspect":
                    title = "巡检巡查 "
                    break
                case "setting":
                    title = "系统设置 "
                    break
                default:
                    title = "智慧消防安全监管平台"
                    break
            }
            return title
        },
    },
    data() {
        return {
            // isCollapsed: false,
            // winH:500,
            resizeTimer: null,
            backTopVisible: false,
            isHome: this.$route.path == "/",
            titleList: [],
        }
    },
    methods: {
        getWinSize() {
            this.winH = document.documentElement.clientHeight || document.body.clientHeight
        },
        resizeEv() {
            clearTimeout(this.resizeTimer)
            this.resizeTimer = setTimeout(() => {
                this.getWinSize()
            }, 500)
        },
        exitAction() {
            this.$post("/auth/logout").then(
                (data) => {
                    //
                },
                (err) => {
                    this.loading = false
                    if (err.msg) {
                        this.$alert(err.msg, "提示", { center: true })
                    }
                }
            )
            this.$store.commit("setSiteInfos", null)
            this.$router.replace("/login")
        },
        getTitle() {
            if (!this.siteInfos || !this.siteInfos.homeId) return
            this.$get("/systemsetup/systemsetup/getTextTitle", {
                homeId: this.siteInfos.homeId,
            }).then(
                (res) => {
                    let list = res.data
                    let defaultList = this.config.defaultTitleList
                    let temp = []
                    defaultList.forEach((item) => {
                        let exist = list.filter(
                            (titem) => item.deviceClassify == titem.deviceClassify
                        )
                        if (exist.length > 0) {
                            temp.push(exist[0])
                        } else {
                            temp.push(item)
                        }
                    })
                    this.titleList = temp
                },
                (err) => {
                    this.closeFullLoading()
                    if (err.msg) {
                        this.$message.error(err.msg)
                    }
                }
            )
        },
    },
    created() {
        this.getTitle()
    },
    mounted() {
        // window.addEventListener('resize',this.resizeEv,false)
    },
    destroyed() {
        // window.removeEventListener("resize", this.resizeEv, false)
    },
}
</script>
